<?php
class LoginController extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('user_model');
	}
	function index(){
		$this->session->sess_destroy();                
		$dataprofile = array(
			'page_title' => 'Login',
			'name' => $this->session->userdata('name')			
		);
		
		$content = array(
			'content' => 'homepage/login'			
		);
		$this->template->load('templates/logintemplate',$content,$dataprofile);
	}
	
	function loginProcess(){
		$rule_config = array(
			array(
				'field'=>'login',
				'label'=>'User Name',
				'rules'=>'trim|required|xss_clean|encode_php_tags|prep_for_form'
			),
			array(
				'field'=>'password',
				'label'=>'Password',
				'rules'=>'trim|required|xss_clean|encode_php_tags|prep_for_form'
			)
		);
		$this->form_validation->set_rules($rule_config);
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$session_var = $this->session->userdata('userid');
		if($this->input->post(NULL,true) || (empty($session_var))){
			if(($this->form_validation->run() == false)){
				$dataprofile = array(
					'page_title' => 'Login',
					'name' => $this->session->userdata('name')			
				);
				$content = array(
					'content' => 'homepage/login'			
				);				
				$this->template->load('templates/logintemplate',$content,$dataprofile);
			}else{
				$this->user_model->setUserId($_POST['login']);
				$this->user_model->setPassword(md5($_POST['password']));
				if($this->user_model->getLogin()){
					$arr_sess = array(
						'userid' => $this->user_model->getUserId(),
						'fullname' => $this->user_model->getFullName(),
						'grade' => $this->user_model->getGrade()
					);
					$array_currsession = array('userid'=>'','fullname'=>'','grade'=>'');
					$this->session->unset_userdata($array_currsession);
					$this->session->set_userdata($arr_sess);
					
					$data = array(
						'page_title' => 'Home',
						'name' => $this->user_model->getFullName()						
					);
					$partials = array(
						'content'=> 'homepage/home'
					);
					$this->template->load('templates/default',$partials,$data);
				}else{
					$dataprofile = array(
						'page_title' => 'Login',
						'message' => 'Maaf Username dan/atau Password Login Anda Salah'		
					);
					$content = array(
						'content' => 'homepage/login'			
					);
					$this->template->load('templates/logintemplate',$content,$dataprofile);
				}				
			}
		}
	}
	
	function logoutProcess(){
		$array_currsession = array('userid'=>'','fullname'=>'','grade'=>'');
        $this->session->unset_userdata($array_currsession);             
		$dataprofile = array(
			'page_title' => 'Login',
			'message' => 'Anda telah logout dari sistem.'
		);
		$content = array(
			'content' => 'homepage/login'			
		);
		$this->template->load('templates/logintemplate',$content,$dataprofile);
	}
}